package com.example.baseframework.lottery.ui.fragment

import android.util.Log
import android.view.*
import androidx.fragment.app.viewModels
import com.example.baseframework.databinding.FragmentLotteryBinding
import com.example.imlotterytool.repository.Status
import com.example.imlotterytool.util.*
import com.example.imlotterytool.viewmodel.LotteryViewModel
import com.google.android.material.snackbar.Snackbar
import com.xh.baselibrary.fragment.BaseVBFragment

/**
@author Anthony.H
@date: 2021/5/14
@desription:
 */
class LotteryFragment : BaseVBFragment<FragmentLotteryBinding>() {

    companion object {
        private const val TAG = "LotteryFragment"
    }
    private val lotteryViewModel by viewModels<LotteryViewModel>(
        factoryProducer = { InjectorUtil.getLotteryViewModelFatory(requireContext().applicationContext) }
    )


    override fun onContentViewInit(contentView: View) {
        super.onContentViewInit(contentView)
        mViewContainer.rgLotteryTypeLine1.setOnCheckedChangeListener { group, checkedId ->
            selectType(checkedId)
        }
        mViewContainer.rgLotteryTypeLine2.setOnCheckedChangeListener { group, checkedId ->
            selectType(checkedId)
        }

        lotteryViewModel.lotteryLiveData.observe(viewLifecycleOwner) { resource ->
                when (resource.status) {
                    Status.LOADING -> updateSnackBarText("正在获取数据...")
                    Status.SUCCESS -> {
                        val data = resource.data
                        data?.let { data ->
                            val dataList = data.list
                            dataList?.let {
                                updateSnackBarText("获取数据成功！")
                                //显示数据
                                when (lotteryType) {
                                    LOTTERY_TYPE_SSQ -> {
                                        mViewContainer.lottertView.isShowBrokenLine = false
                                        mViewContainer.lottertView.setBitCountList(listOf(IntRange(0, 32), IntRange(33, 48)))
                                    }
                                    LOTTERY_TYPE_DLT -> {
                                        mViewContainer.lottertView.isShowBrokenLine = false
                                        mViewContainer.lottertView.setBitCountList(listOf(IntRange(0, 34), IntRange(35, 47)))
                                    }
                                    LOTTERY_TYPE_FCSD, LOTTERY_TYPE_PL3 -> {
                                        mViewContainer.lottertView.isShowBrokenLine = true
                                        mViewContainer.lottertView.setBitCountList(listOf(IntRange(0, 9), IntRange(10, 19), IntRange(20, 29)))
                                    }
                                    LOTTERY_TYPE_PL5 -> {
                                        mViewContainer.lottertView.isShowBrokenLine = true
                                        mViewContainer.lottertView.setBitCountList(
                                            listOf(IntRange(0, 9), IntRange(10, 19), IntRange(20, 29), IntRange(30, 39), IntRange(40, 49))
                                        )
                                    }

                                    LOTTERY_TYPE_7XC -> {
                                        mViewContainer.lottertView.isShowBrokenLine = true
                                        mViewContainer.lottertView.setBitCountList(
                                            listOf(
                                                IntRange(0, 9),
                                                IntRange(10, 19),
                                                IntRange(20, 29),
                                                IntRange(30, 39),
                                                IntRange(40, 49),
                                                IntRange(50, 59),
                                                IntRange(60, 74)
                                            )
                                        )

                                    }
                                    else -> {
                                        mViewContainer.lottertView.isShowBrokenLine = false

                                    }
                                }

                                mViewContainer.lottertView.refreshData(it.sortedByDescending { it.issues }, getTitleListByLotteryType(data.lotteryId))
                                mViewContainer.refreshLayout.finishRefresh()
                            }
                        }
                    }
                    Status.ERROR -> {
                        mViewContainer.let {
                            var msg = "error:"
                            resource.message?.let { message -> msg = message }
                            updateSnackBarText(msg)
                        }
                    }
                }
        }
        mViewContainer.refreshLayout.setOnRefreshListener {
            lotteryViewModel.requestHistory(lotteryType)
        }
        switchTo(LOTTERY_TYPE_FCSD)
    }

    private fun selectType(checkedId: Int) {
        mViewContainer.rbFcsd.isSelected = false
        mViewContainer.rbSsq.isSelected = false
        mViewContainer.rbPl3.isSelected = false
        mViewContainer.rbPl5.isSelected = false
        mViewContainer.rbQxc.isSelected = false
        mViewContainer.rbCjdlt.isSelected = false
        when(checkedId){
            mViewContainer.rbFcsd.id ->{
                mViewContainer.rbFcsd.isSelected = true
                switchTo(LOTTERY_TYPE_FCSD)
            }
            mViewContainer.rbSsq.id ->{
                mViewContainer.rbSsq.isSelected = true
                switchTo(LOTTERY_TYPE_SSQ)
            }
            mViewContainer.rbPl3.id ->{
                mViewContainer.rbPl3.isSelected = true
                switchTo(LOTTERY_TYPE_PL3)
            }
            mViewContainer.rbPl5.id ->{
                mViewContainer.rbPl5.isSelected = true
                switchTo(LOTTERY_TYPE_PL5)
            }
            mViewContainer.rbQxc.id ->{
                mViewContainer.rbQxc.isSelected = true
                switchTo(LOTTERY_TYPE_7XC)
            }
            mViewContainer.rbCjdlt.id ->{
                mViewContainer.rbCjdlt.isSelected = true
                switchTo(LOTTERY_TYPE_DLT)
            }
        }
    }

    private var lotteryType = ""
    private fun switchTo(lotteryId: String) {
        Log.d(TAG, "switchTo ---> $lotteryId")
        lotteryType = lotteryId
        lotteryViewModel.requestHistory(lotteryId)
    }


    private fun updateSnackBarText(message: String) {
        mViewContainer.let {
            Snackbar.make(it.lottertView, message, Snackbar.LENGTH_SHORT).show()
        }
    }
}

